Skip to content

Rebase stacks onto the latest remote trunk - #330

Open
skarim wants to merge 2 commits into
mainfrom
skarim/trunk-rebase-fix
Open

Rebase stacks onto the latest remote trunk#330
skarim wants to merge 2 commits into
mainfrom
skarim/trunk-rebase-fix

Conversation

@skarim

@skarim skarim commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

Summary

gh stack rebase and gh stack sync could report success while leaving a stack on a stale trunk, particularly when the local trunk was checked out in another worktree, had diverged from the remote, or the fetch failed.

This change fetches the trunk explicitly and rebases onto the fresh remote ref when the local branch cannot be updated, while preserving locally-ahead and local-only trunks. It also treats rebases that git refuses to start as failures and verifies the resulting ancestry before reporting success or pushing from sync.

Testing

  • go vet ./...
  • go test -race -count=1 ./...
  • Validated rebase and sync against a six-layer stack with main locked in another worktree
  • Validated failed-fetch and deleted-trunk behavior without moving stack branches

Fetch the configured trunk explicitly before sync or rebase and use that
fetched ref whenever the local trunk cannot be safely updated, while
preserving local-only and locally-ahead trunks. Fail instead of reporting
success when the fetch or rebase never starts, carry the resolved trunk
through conflict recovery, and verify the resulting ancestry before sync
pushes or either command reports success.
@skarim
skarim marked this pull request as ready for review July 27, 2026 20:20
Copilot AI review requested due to automatic review settings July 27, 2026 20:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates rebase and sync flows to target the freshly fetched remote trunk and fail safely when rebases cannot start or ancestry verification fails.

Changes:

  • Resolves and fetches an authoritative trunk target.
  • Adds fatal rebase-start error handling and post-rebase verification.
  • Extends recovery state, mocks, and integration tests.
Show a summary per file
File Description
cmd/rebase.go Uses resolved trunk targets and verifies ancestry.
cmd/rebase_test.go Updates rebase mocks.
cmd/sync.go Rebases and verifies before pushing.
cmd/sync_test.go Updates sync ancestry behavior.
cmd/trunk_target_test.go Tests trunk resolution and failure paths.
cmd/utils.go Implements trunk targeting and stack verification.
cmd/utils_test.go Updates helper tests.
internal/git/git.go Adds rebase-start errors and delegates.
internal/git/gitops.go Adds exact branch fetch and upstream lookup.
internal/git/mock_ops.go Extends Git mocks.
internal/git/rebase_start_test.go Adds Git integration coverage.
internal/modify/apply.go Handles refused modify rebases as fatal.

Review details

Comments suppressed due to low confidence (1)

cmd/utils.go:1178

  • A start refusal on a later branch can occur after earlier branches were successfully rebased. Returning only Err leaves those earlier refs rewritten without recovery metadata or restoration, so rerunning may replay commits from the old parent history. Keep the error fatal, but restore OriginalRefs in both callers or persist a recoverable partial-cascade state.
				if git.IsRebaseStartError(rebaseErr) {
					return cascadeRebaseResult{
						Rebased: result.Rebased,
						Err:     fmt.Errorf("could not start rebase of %s onto %s: %w", br.Branch, base, rebaseErr),
					}
  • Files reviewed: 12/12 changed files
  • Comments generated: 4
  • Review effort level: Medium

Comment thread cmd/utils.go
Comment thread cmd/rebase.go
Comment thread internal/modify/apply.go
Comment thread cmd/utils.go
Roll back branches already rewritten when a later rebase cannot start or
final ancestry verification fails, preventing retries from replaying stale
history. Preserve retryable modify state without repeating completed work,
and add regression coverage for remote-qualified trunk normalization.

@ktravers ktravers left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excited to get you a rebase API endpoint you can use asap 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gh stack rebase doubles remote prefix for remote-qualified trunk gh stack sync merged approved PRs into bottom branch

3 participants